package com.cr.front.order.controller;

import com.cr.common.response.JsonResult;
import com.cr.front.order.pojo.TimeRange;
import com.cr.front.order.pojo.dto.*;
import com.cr.front.order.pojo.vo.OrderVO;
import com.cr.front.order.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Api(tags = "订单管理")
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    OrderService orderService;
    @ApiOperation(value = "保存订单")
    @PostMapping("saveOrder")
    public JsonResult saveVehicle(OrderSaveParam orderSaveParam){
        log.debug("保存的订单参数:orderSaveParam={}",orderSaveParam);
        String orderId = orderService.saveOrder(orderSaveParam);
        return JsonResult.success(orderId);
    }

    @ApiOperation(value = "查询一辆车订单列表")
    @GetMapping("vehicle")
    public JsonResult listOrderPeriods(OrderQuery orderQuery) {
        List<OrderVO> list = orderService.listOrderByVehicleId(orderQuery);
        return JsonResult.success(list);
    }

    @ApiOperation(value = "查询用户订单列表")
    @GetMapping("user")
    public JsonResult listOrderByUSerId(UserOrderQuery userOrderQuery) {
        List<OrderVO> list = orderService.listOrderByUSerId(userOrderQuery);
        return JsonResult.success(list);
    }

    @ApiOperation(value = "查询一辆车可用时间列表")
    @GetMapping("vehicle/availabletime")
    public JsonResult listAvailableTime(OrderQuery orderQuery) {
        List<List<TimeRange>>list = orderService.listAvailableTime(orderQuery);
        return JsonResult.success(list);
    }

    @ApiOperation(value = "查询订单详细")
    @GetMapping("detail/{orderId}")
    public JsonResult getOrderDetail(@PathVariable String orderId) {
        OrderVO orderVO = orderService.getOrderDetail(orderId);
        return JsonResult.success(orderVO);
    }
    //进入支付页面使用
    @ApiOperation(value = "查询支付订单详细")
    @GetMapping("{orderId}")
    public JsonResult getPaymentOrderDetail(@PathVariable String orderId) {
        OrderVO orderVO = orderService.getPaymentOrderDetail(orderId);
        return JsonResult.success(orderVO);
    }

    @ApiOperation(value = "支付订单")
    @PostMapping("payment/{orderId}")
    public JsonResult payOrder(@PathVariable("orderId") String orderId,@RequestBody @Validated PayOrderParam payOrderParam) {
        orderService.payOrder(orderId,payOrderParam);
        return JsonResult.success();
    }

    @ApiOperation(value = "支付状态检验")
    @PostMapping("validatepayment/{orderId}")
    public JsonResult validatePayment(@PathVariable("orderId") String orderId) {
        String paymentStatus = orderService.validatePayment(orderId);
        return JsonResult.success(paymentStatus);
    }

    @ApiOperation(value = "取消订单")
    @PostMapping("cancel/{orderId}")
    public JsonResult cancelOrder(@PathVariable("orderId") String orderId, @RequestBody @Validated OrderCancelParam orderCancelParam) {
        orderService.cancelOrder(orderId,orderCancelParam);
        return JsonResult.success();
    }

    @ApiOperation(value = "还车完成订单")
    @PostMapping("complete/{orderId}")
    public JsonResult completeOrder(@PathVariable("orderId") String orderId) {
        orderService.completeOrder(orderId);
        return JsonResult.success();
    }


}
